<?php
namespace app\home\controller;

use app\common\controller\Home;
use app\common\model\Subject\Subject;
use app\common\model\Subject\Teacher\Follow;
use app\common\model\Subject\Teacher\Teacher;
use think\Cookie;
use think\Db;
use think\Request;

class Educator extends Home{
    public function __construct(Request $request)
    {
        $this->noLogin = ['index'];
        parent::__construct($request);
    }
    public function index(Request $request)
    {
        if($request->isGet()){
            return $this->fetch();
        }
        $pageNum = $request->post('pageNum')?$request->post('pageNum'):1;
        $pageSize = $request->post('pageSize')?$request->post('pageSize'):5;
        $keyword = $request->post('keyword')?trim($request->post('keyword')):'';
        $teacher = Teacher::where('name','like','%'.$keyword.'%')
                ->order('createtime','desc')
                ->limit(($pageNum-1)*$pageSize,$pageSize)
                ->select();

        $page = [];
        $page['curPageData'] = $teacher;
        $page['totalPage'] = ceil(Teacher::where('name','like','%'.$keyword.'%')->count()/$pageSize);
        return $page;
    }
    public function teacherInfo(Request $request)
    {
        $teacherid = $request->param('id');
        if(!$teacherid){
            return $this->error('请求失敗','/home/educator');
        }
        $teacher = Teacher::where('id',$teacherid)->select();
        // halt($teacher);
        if(!$teacher){
            return $this->error('请求失敗','/home/educator');
        }
        $busid = $this->business['id'];
        $follow = Follow::where(['teacherid'=>$teacherid,'busid'=>$busid])->find();
        
        if($request->isGet()){
            $isFollow = true && $follow;
            $this->assign('teacher',$teacher->toArray()[0]);
            $this->assign('isFollow',$isFollow);
            // halt($teacher->toArray()[0]);
            return $this->fetch('teacher_info');
        }
        $status = ['code'=>0];
        if($request->isPost()){
            if($follow){
                $result = $follow->delete();
                if($result){
                    $status['code'] = 1;
                    $status['msg'] = '取消成功';
                    $status['isFollow'] = false;
                }else{
                    $status['msg'] = '取消失败';
                }
            }else{
                $data = [
                    'teacherid'=>$teacherid,
                    'busid'=>$busid,
                ];
                $result = Follow::create($data);
                if($result){
                    $status['code'] = 1;
                    $status['msg'] = '关注成功';
                    $status['isFollow'] = true;
                }else{
                    $status['msg'] = '关注失败';
                }
            }
            return $status;
        }
        
    }
    public function follow(Request $request)
    {
        $id = $request->param('id');
        $teacher = Teacher::where('id',$id)->find();
        if(!$teacher){
            $this->error('老师不存在');
        }
        if($request->isGet()){
            $this->assign('id',$id);
            return $this->fetch();
        }
        $pageNum = $request->post('pageNum')?$request->post('pageNum'):1;
        $pageSize = $request->post('pageSize')?$request->post('pageSize'):5;
        $keyword = $request->post('keyword')?trim($request->post('keyword')):'';
        $where = ['teacherid'=>$id,'nickname'=>['like','%'.$keyword.'%']];
        // $busid = Db::table('fa_business')->select();
        // halt($busid);
        $follows = Follow::with(['business'])
                ->where($where)
                ->order('follow.createtime','desc')
                ->limit(($pageNum-1)*$pageSize,$pageSize)
                ->select();
        
        // halt($follows->toArray());
        $page = [];
        $page['curPageData'] = $follows;
        $page['totalPage'] = ceil(Follow::with(['business'])->where($where)->count()/$pageSize);
        return $page;
    }
}